﻿package fwk.panelLoading {	

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.display.StageAlign;

	public class LoadingDisplayer extends MovieClip
	{
		
		public var panel:MovieClip;
		public var _container:MovieClip;
		
		public var onProgressFunction:Function;
		public var onNextItemFunction:Function;
		
		
		
		//-----------------------------------------------------------------------
		function LoadingDisplayer(mc:MovieClip = null){
			if(mc != null) panel = mc;
			else panel = this;
			
			
			
		}
		
		//-----------------------------------------------------------------------
		public function set container(val:MovieClip){
			_container = val;
		}
		
		//-----------------------------------------------------------------------
		public function ready(){		
			dispatchEvent(new Event("onReady"));
		}
		//-----------------------------------------------------------------------
		public function display(){		
			
			if (_container.stage.align == StageAlign.TOP_LEFT)
			{
					
				_container.stage.addEventListener(Event.RESIZE, onResize);
				onResize();
				
			}
			
			
			panel.gotoAndPlay("open");
			_container.stage.addChild(panel);
		}
		
		private function onResize(e:Event = null):void 
		{
			// position
			panel.fond.width = 1;
			panel.fond.height = 1;
			panel.fond.x = 0;
			panel.fond.y = 0;
			
			panel.x = Math.floor(_container.stage.stageWidth/2 - panel.width/2);
			panel.y = Math.floor(_container.stage.stageHeight/2 - panel.height/2);
			
			// position fond
			panel.fond.x = - panel.x;
			panel.fond.y = - panel.y;
			
			//taille fond			
			panel.fond.width = _container.stage.stageWidth;
			panel.fond.height = _container.stage.stageHeight;
		}
		//-----------------------------------------------------------------------
		//------- loading progress
		//-----------------------------------------------------------------------
		public function setProgress(percent:Number, percentCurrent:Number = NaN){
			if(this.onProgressFunction != null) this.onProgressFunction(percent, percentCurrent);
		}
		//-----------------------------------------------------------------------
		private function onComplete(evt:Event){
			hide();
		}
		//-----------------------------------------------------------------------
		public function hide(){
			panel.stop();
			panel.gotoAndPlay("close");
		}
		public function close()
		{
		_container.stage.removeEventListener(Event.RESIZE, onResize);
			
			dispatchEvent(new Event("closed"));
			_container.stage.removeChild(panel);
		}
		public function setLabel(txt:String){			
			if(this.onNextItemFunction != null) this.onNextItemFunction(txt);
		}

	}
}
